import urllib
import urllib.request
import random


uapools = ["Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
           "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.75 Safari/537.36",
           "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)",
           "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 TheWorld 7"]


def UA():
    opener = urllib.request.build_opener()
    thisua = random.choice(uapools)
    ua = ("User-Agent", thisua)
    opener.addheaders = [ua]
    urllib.request.install_opener(opener)
    print("当前使用UA：" + str(thisua))


def set_header(header):
    opener = urllib.request.build_opener()
    opener.addheaders = header
    urllib.request.install_opener(opener)
    print("当前使用head：" + str(header))


def get_base_url(refer_url):
    """
    获取网址的根网址
    :param refer_url: 网址
    :return: 网址的根网址
    """
    o = urllib.parse.urlparse(refer_url)
    base_url = o.scheme + "://" + o.netloc
    return base_url

